package cn.stylefeng.roses.kernel.pay.modular.controller;

import cn.hutool.core.io.IoUtil;
import cn.stylefeng.roses.kernel.pay.modular.entity.Order;
import cn.stylefeng.roses.kernel.pay.modular.pojo.NotifyRequest;
import cn.stylefeng.roses.kernel.pay.modular.pojo.OrderDTO;
import cn.stylefeng.roses.kernel.pay.modular.pojo.OrderRequest;
import cn.stylefeng.roses.kernel.pay.modular.service.OrderService;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * 订单控制器
 *
 * @author stylefeng
 * @date 2021/06/23 22:28
 */
@RestController
@ApiResource(name = "订单")
public class OrderController {

    @Resource
    private OrderService orderService;

    /**
     * 下单接口，生成订单
     *
     * @author stylefeng
     * @date 2021/06/23 22:28
     */
    @GetResource(name = "下单接口", path = "/order/buy", requiredPermission = false)
    public ResponseData<Order> buy(@Validated(OrderRequest.buy.class) OrderRequest orderRequest) {
        return new SuccessResponseData<>(orderService.createOrder(orderRequest));
    }

    /**
     * 支付界面，生成支付二维码
     *
     * @author stylefeng
     * @date 2021/06/23 22:28
     */
    @GetResource(name = "支付接口", path = "/order/pay", requiredPermission = false)
    public ResponseData<String> pay(@Validated(OrderRequest.pay.class) OrderRequest orderRequest) {
        return new SuccessResponseData<>(orderService.pay(orderRequest));
    }

    /**
     * 聚合支付，支付成功回调
     *
     * @author stylefeng
     * @date 2021/06/23 22:28
     */
    @ApiResource(name = "聚合支付，支付成功回调", path = "/order/theNotify",
            method = {RequestMethod.GET, RequestMethod.POST}, requiredPermission = false, requiredLogin = false)
    public void mergeNotify(NotifyRequest notifyRequest, HttpServletResponse response) {

        orderService.payNotify(notifyRequest);

        PrintWriter printWriter = null;
        try {
            response.setContentType("text/plain;charset=utf-8");
            printWriter = response.getWriter();
            printWriter.write("SUCCESS");
            printWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IoUtil.close(printWriter);
        }
    }

    /**
     * 查询订单状态
     *
     * @author stylefeng
     * @date 2021/06/23 22:28
     */
    @GetResource(name = "查询订单状态", path = "/order/getOrderStatus", requiredPermission = false)
    public ResponseData<Integer> getOrderStatus(@Validated(OrderRequest.orderStatus.class) OrderRequest orderRequest) {

        Order detail = orderService.detail(orderRequest);

        // 直接返回状态
        return new SuccessResponseData<>(detail.getState());
    }

    /**
     * 获取订单列表
     *
     * @author stylefeng
     * @date 2021/06/23 22:28
     */
    @GetResource(name = "获取订单列表", path = "/order/list", requiredPermission = false)
    public ResponseData<List<OrderDTO>> list(OrderRequest orderRequest) {
        return new SuccessResponseData<>(orderService.findList(orderRequest));
    }

    /**
     * 取消订单
     *
     * @author stylefeng
     * @date 2021/06/23 22:28
     */
    @PostResource(name = "取消订单", path = "/order/cancelOrder", requiredPermission = false)
    public ResponseData<?> cancelOrder(@RequestBody @Validated(OrderRequest.detail.class) OrderRequest orderRequest) {
        orderService.cancelOrder(orderRequest);
        return new SuccessResponseData<>();
    }

    /**
     * 删除订单
     *
     * @author stylefeng
     * @date 2021/06/23 22:28
     */
    @PostResource(name = "删除订单", path = "/order/deleteOrder", requiredPermission = false)
    public ResponseData<?> deleteOrder(@RequestBody @Validated(OrderRequest.detail.class) OrderRequest orderRequest) {
        orderService.deleteOrder(orderRequest);
        return new SuccessResponseData<>();
    }

}
